package com.lg.partition30;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/5/17 16:13
 */
public class P2986USACO10MAR_GreatCowGatheringG {

    private static final int N = 100010, M = 2 * N;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M], hw = new int[N];
    private static long[] size = new long[N], f1 = new long[N], f2 = new long[N];
    private static int n, idx = 0, cnt = 0;
    private static long ans = (long) 1e18;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        Arrays.fill(h, -1);
        for (int i = 1; i <= n; i++) size[i] = hw[i] = nextInt();
        for (int i = 0; i < n - 1; i++) {
            int a = nextInt(), b = nextInt(), c = nextInt();
            add(a, b, c);
            add(b, a, c);
        }
        dfs1(1, 0);
        f2[1] = f1[1];
        ans = Math.min(ans, f2[1]);
        dfs2(1, 0);
        writer.println(ans);
        writer.flush();
    }

    private static void dfs2(int u, int fa) {
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (v == fa) continue;
            f2[v] = f2[u] + (cnt - size[v]) * w[i] - size[v] * w[i];
            ans = Math.min(ans, f2[v]);
            dfs2(v, u);
        }
    }

    private static void dfs1(int u, int fa) {
        cnt += hw[u];
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (v == fa) continue;
            dfs1(v, u);
            size[u] += size[v];
            f1[u] += f1[v] + size[v] * w[i];
        }
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
